# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     day161_ptqt5_moveButton
   Description :
   Author :       fuml
   date：          2019/9/23
-------------------------------------------------
   Change Activity:
                   2019/9/23:his is a simple drag and
drop example.
左键 按下按钮 打印 press
右键移动按钮 --没有看明白
-------------------------------------------------
"""
import  sys
from PyQt5.QtWidgets import  QPushButton,QWidget,QApplication
from PyQt5.QtCore import Qt,QMimeData
from PyQt5.QtGui import QDrag

class MyButton(QPushButton):
    def __init__(self,title,parent):
        super().__init__(title,parent)

    def mouseMoveEvent(self, e):
        if e.buttons()!=Qt.RightButton:
            return
        mimedata = QMimeData()

        drag = QDrag(self)
        drag.setMimeData(mimedata)
        drag.setHotSpot(e.pos()-self.rect().topLeft())

        dragAction = drag.exec_(Qt.MoveAction)

    def mousePressEvent(self, e):
        QPushButton.mousePressEvent(self,e)
        if e.button() == Qt.LeftButton:
            print('press')

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setAcceptDrops(True)
        self.button = MyButton('Button',self)
        self.button.move(100,65)

        self.setWindowTitle('Click or Move')
        self.setGeometry(300,300,280,150)

    def dragEnterEvent(self, e):
        e.accept()

    def dropEvent(self, e):
        positon = e.pos()
        self.button.move(positon)
        e.setDropAction(Qt.MoveAction)
        e.accept()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex= Example()
    ex.show()
    sys.exit(app.exec_())